package com.octopus.crontab;

import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.Timer;

public class CrontabTimer {
	
	private Timer timer;
	
	private Map<String, Object> context;
	
	public CrontabTimer(String name, boolean isDaemon){
		timer = new Timer(name, isDaemon);
		context = new Hashtable<String, Object>();
	}
	
	public Date getRunFirstTime(){
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.MINUTE, (c.get(Calendar.MINUTE)+1));
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}
	
	public void cancel(){
		timer.cancel();
	}
	
	public void addTask(CrontabTask task){
		timer.schedule(task, getRunFirstTime(), 60000);
		task.setContext(context);
	}
}
